package com.tanhua.api;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.sso.pojo.Comment;
import com.tanhua.sso.pojo.Movement;
import com.tanhua.sso.pojo.Video;
import com.tanhua.sso.utils.CommentType;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentsServiceApiImpi implements CommentsServiceApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    //新增评论,点赞
    @Override
    public Integer save(Comment newComment) {
        ObjectId publishId = newComment.getPublishId();
        //查看发布者id
        Movement movement = mongoTemplate.findById(publishId, Movement.class);
        Long userId = movement.getUserId();
        newComment.setPublishUserId(userId);
        //新增到Comment
        mongoTemplate.save(newComment);
        //TYPE==1,喜欢
        Update update=new Update();
        if (newComment.getCommentType()== CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if (newComment.getCommentType()==CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        Criteria criteria = Criteria.where("id").is(publishId);
        Query query = Query.query(criteria);
        //设置更新参数
        FindAndModifyOptions findAndModifyOptions=new FindAndModifyOptions();
        //更新并返回最新值
        findAndModifyOptions.returnNew(true);
        //返回更新的对象
        Movement andModify = mongoTemplate.findAndModify(query, update, findAndModifyOptions, Movement.class);
        //点赞数
        if (newComment.getCommentType()== CommentType.LIKE.getType()){
            return andModify.getLikeCount();
            //评论数
        }else if (newComment.getCommentType()==CommentType.COMMENT.getType()){
            return andModify.getCommentCount();
        }else {
            //喜欢数
            return andModify.getLoveCount();
        }
    }

    /*评论列表*/
    @Override
    public List<Comment> findCommentByMovementId(Integer page, Integer pagesize, String movementId) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(CommentType.COMMENT.getType());
        //分页
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip((page - 1) * pagesize).limit(pagesize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        return commentList;
    }

    @Override
    public Integer findMyLike(Long nowId, ObjectId id) {
        //查看当前动态,我是否点赞
        Criteria criteria = Criteria.where("publishId").is(id).and("userId").is(nowId).and("commentType").is(CommentType.LIKE.getType());
        List<Comment> commentList = mongoTemplate.find(Query.query(criteria), Comment.class);
        if (ObjectUtil.isNotEmpty(commentList)){
            return 1;
        }else {
            return 0;
        }
    }

    //查看,评论或点赞或喜欢
    @Override
    public List<Comment> findType(Long userId, int type,Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("publishUserId").is(userId);
        Criteria criteria1 =null;
        //点赞
        if (type==CommentType.LIKE.getType()){
            criteria1 = criteria.and("commentType").is(type);
            //喜欢
        }else if (type==CommentType.LOVE.getType()){
            criteria1 = criteria.and("commentType").is(type);
            //评论
        }else {
            criteria1 = criteria.and("commentType").is(type);
        }
        Query query= Query.query(criteria1).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query,Comment.class);
        return commentList;
    }

    //视频点赞
    @Override
    public void saveVideoLike(Comment comment) {
        ObjectId publishId = comment.getPublishId();
        //查看发布者id
        Video byId = mongoTemplate.findById(publishId, Video.class);
        Long userId = comment.getUserId();
        comment.setPublishUserId(userId);
        //新增到Comment
        mongoTemplate.save(comment);
        Update update=new Update();
        update.inc("likeCount",1);
        Criteria criteria = Criteria.where("id").is(publishId);
        Query query = Query.query(criteria);
        mongoTemplate.updateFirst(query,update,Video.class);
    }

    //取消点赞
    @Override
    public void disVideoLike(ObjectId id, Long userId) {
        Criteria criteria = Criteria.where("publishId").is(id).and("userId").is(userId).and("commentType").is(CommentType.LIKE.getType());
        //找出已点赞的记录,并删除
        Comment one = mongoTemplate.findOne(Query.query(criteria), Comment.class);
        mongoTemplate.remove(one);
        //修改当前动态的点赞数
        Query query = Query.query(Criteria.where("id").is(id));
        Update update=new Update();
        update.inc("likeCount",-1);
        mongoTemplate.updateFirst(query,update,Video.class);

    }

    //发布视频评论
    @Override
    public void saveVideoComment(Comment newComment) {
        ObjectId publishId = newComment.getPublishId();
        //查看发布者id
        Video byId = mongoTemplate.findById(publishId, Video.class);
        Long userId = newComment.getUserId();
        newComment.setPublishUserId(userId);
        mongoTemplate.save(newComment);
        Update update=new Update();
        update.inc("commentCount",1);
        Criteria criteria = Criteria.where("id").is(publishId);
        Query query = Query.query(criteria);
        mongoTemplate.updateFirst(query,update,Video.class);

    }


}
